home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / print / p1123mz4.zip / VARRAY.H < prev    next >
C/C++ Source or Header  |  1994-02-27  |  17KB  |  450 lines

  1. #ifndef VARRAY_H
  2. #define VARRAY_H
  3.  
  4. //      This is a template for a virtual array of type T.
  5.  
  6. #include <stdio.h>
  7. #include <dos.h>
  8.  
  9. #ifndef TRUE
  10. #define TRUE -1
  11. #endif
  12. #ifndef FALSE
  13. #define FALSE 0
  14. #endif
  15.  
  16. template <class T>
  17. class varray
  18.   {
  19.     private:
  20.       int          expanded_memory_allocated(void);
  21.                     // Allocated expanded memory.  If this can't be done,
  22.                     // an attempt will be made to use a disk instead.
  23.       unsigned int expanded_memory_handle;
  24.       int          free_expanded_memory;
  25.       int          free_disk_memory;
  26.                     // The temporary file used for virtual memory needs to be
  27.                     // freed.
  28.       void         free_memory(void);
  29.                     // Free real memory used for pages, etc.
  30.       int          free_real_memory;
  31.                     // Real memory needs to be freed.
  32.       int          map_expanded_memory(int physical_page,long logical_page);
  33.                     // Associate a physical page in the physical page frame
  34.                     // with a logical page of expanded memory.
  35.       int          memory_allocated(void);
  36.                     // Allocate real memory for pages, etc.
  37.       long         num_elements_in_array;
  38.                     // Number of elements in the array.
  39.       unsigned int num_elements_per_page;
  40.                     // Number of array elements in a page.
  41.       int          num_real_pages;
  42.                     // Number of pages kept in real memory.
  43.       char         *page_frame_ptr;
  44.                     // Pointer to expanded memory physical frame.
  45.       T            **real_page;
  46.                     // A page in real memory.
  47.       long         *starting_element_num;
  48.                     // Index of first array element in page in real memory.
  49.       FILE         *vm;
  50.                     // Temporary file used for virtual memory.
  51.       long         *vm_access;
  52.                     // "When" a page in real memory was last accessed.
  53.       long         vm_access_num;
  54.                     // Current "time".
  55.     public:
  56.       int allocated(void) {return (free_real_memory &&
  57.            (free_expanded_memory || free_disk_memory));}
  58.  
  59.           varray(T& initialized_element,long element_count,
  60.            int real_page_count=4,unsigned int page_size=32768);
  61. //      Construct a virtual array of type T.  The arguments are as follow:
  62. //
  63. //           initialized_element -- an initialized element of type T.
  64. //
  65. //           element_count -- the number of elements in the array.
  66. //   
  67. //           real_page_count -- the number of pages to be kept in memory.
  68. //      This should be at least as large as the maximum number of elements
  69. //      of the array referenced at once.  Small values result in too much
  70. //      paging; large values result in too much overhead searching for the
  71. //      page containing an element.
  72. //
  73. //           page_size -- the number of bytes in a page.  This must be at least
  74. //      sizeof(T).  This is used only when a disk is used for virtual memory.
  75. //      (When expanded memory is used, the page size is effectively 32K.)  32K 
  76. //      is probably optimal due to the overhead of searching for a page.
  77.  
  78.           ~varray(void);
  79. //      If expanded memory was used, free it.  If a temporary file was used,
  80. // close (and so delete) it.  If real memory was allocated, free it.
  81.  
  82.       T   *vm_ptr(long element_num);
  83. //      Return a pointer to an element of the array.  When T is a structure, it
  84. // is faster to use this pointer than to overload [] and search virtual memory
  85. // for each component of the structure.
  86.  
  87.       T&  operator[](long element_num) {return *vm_ptr(element_num);}
  88. //     Return an element of the array by overloading [].
  89.   };
  90.  
  91. template <class T>
  92. varray<T>::varray(T& initialized_element,long element_count,int real_page_count,
  93.  unsigned int page_size)
  94. //      Construct a virtual array of type T.  The arguments are as follow:
  95. //
  96. //           initialized_element -- an initialized element of type T.
  97. //
  98. //           element_count -- the number of elements in the array.
  99. //   
  100. //           real_page_count -- the number of pages to be kept in memory.
  101. //      This should be at least as large as the maximum number of elements
  102. //      of the array referenced at once.  Small values result in too much
  103. //      paging; large values result in too much overhead searching for the
  104. //      page containing an element.
  105. //
  106. //           page_size -- the number of bytes in a page.  This must be at least
  107. //      sizeof(T).  This is used only when a disk is used for virtual memory.
  108. //      (When expanded memory is used, the page size is effectively 32K.)  32K 
  109. //      is probably optimal due to the overhead of searching for a page.
  110.   {
  111.     unsigned int element_index;
  112.     long         element_num;
  113.  
  114.     free_real_memory=FALSE;
  115.     free_disk_memory=FALSE;
  116.     free_expanded_memory=FALSE;
  117.     num_real_pages=real_page_count;
  118.     num_elements_in_array=element_count;
  119.     if (sizeof(T) <= 32768)
  120.       {      
  121.         num_elements_per_page=32768/sizeof(T);
  122.         free_expanded_memory=expanded_memory_allocated();
  123.       }
  124.     if (free_expanded_memory)
  125.       if (memory_allocated()) // Allocate space for the active pages.
  126.         {
  127.           free_real_memory=TRUE;
  128.           long expanded_memory_page_num=0L;
  129.           int real_page_num=0;
  130.           vm_access_num=0;
  131.           int successful=TRUE;
  132.           for (element_num=(long) 0;
  133.            ((successful) && (element_num < num_elements_in_array));
  134.            element_num+=((long) num_elements_per_page))
  135.             {
  136.               if (real_page_num < real_page_count)
  137.               // Initialize real page.
  138.                 {
  139.                   vm_access_num++;
  140.                   for (element_index=(unsigned int) 0;
  141.                    element_index < num_elements_per_page;
  142.                    element_index++)
  143.                     real_page[real_page_num][element_index]
  144.                      =initialized_element;
  145.                   vm_access[real_page_num]=vm_access_num;
  146.                   starting_element_num[real_page_num]=element_num;
  147.                   real_page_num++;
  148.                 }
  149.               // Initialize virtual page.
  150.               if (successful=map_expanded_memory(0,expanded_memory_page_num++))
  151.                 {
  152.                   if (successful
  153.                    =map_expanded_memory(1,expanded_memory_page_num++))
  154.                     memcpy((void *) page_frame_ptr,(void *) &(real_page[0][0]),
  155.                      num_elements_per_page*sizeof(T));
  156.                 }
  157.             }
  158.           if (! successful)
  159.             cerr
  160.              << "Fatal error:  there is an unexpected problem with expanded "
  161.              << "memory." << '\n';
  162.         }
  163.       else
  164.         cerr << "Fatal error:  not enough real memory for virtual array." 
  165.          << '\n';
  166.     else
  167.       {
  168.         num_elements_per_page=page_size/sizeof(T);
  169.         if ((vm=tmpfile()) == NULL) // Get a file for the virtual memory.
  170.           cerr << "Fatal error:  cannot open virtual memory." << '\n';
  171.         else
  172.           if (memory_allocated()) // Allocate space for the active pages.
  173.             {
  174.               free_real_memory=TRUE;
  175.               free_disk_memory=TRUE;
  176.               int real_page_num=0;
  177.               vm_access_num=0;
  178.               int successful=TRUE;
  179.               for (element_num=(long) 0;
  180.                ((successful) && (element_num < num_elements_in_array));
  181.                element_num+=((long) num_elements_per_page))
  182.                 {
  183.                   if (real_page_num < real_page_count)
  184.                   // Initialize real page.
  185.                     {
  186.                       vm_access_num++;
  187.                       for (element_index=(unsigned int) 0;
  188.                        element_index < num_elements_per_page;
  189.                        element_index++)
  190.                         real_page[real_page_num][element_index]
  191.                          =initialized_element;
  192.                       vm_access[real_page_num]=vm_access_num;
  193.                       starting